#!/usr/bin/perl
use warnings;
use strict;
die "perl $0 <SNP:genename start end> <SSR:genename start end> <output>\n use for find snp in ssr" if @ARGV ne 3;

my %ssrhash;
#defined ssr place info
open SSR,$ARGV[1] or die "failed to open ssr file";
while(<SSR>){
    chomp;
	my @array = split (/\t/,$_);
	if($ssrhash{$array[0]}){
		$ssrhash{$array[0]}=$ssrhash{$array[0]}.";".$array[1]."-".$array[2];
	}else{
		$ssrhash{$array[0]}=$array[1]."-".$array[2];
	}
}
open OUT,">./$ARGV[2]";
print OUT "gene\tsnp_start\tsnp_end\tssr_start\tssr_end\n";
#find snp in ssr 
open SNP,$ARGV[0] or die "failed to open snp file";
while(<SNP>){
	chomp;
	my @array2 = split(/\t/,$_);
	#fillter snp isn't in ssr
	if($ssrhash{$array2[0]}){
		#get specific unigene ssr place info
		my @a_genessr = split(";",$ssrhash{$array2[0]});
		#get every place then judge
		foreach my $place(@a_genessr){
			my @a_place = split("-",$place);
			my $start = shift @a_place;
			my $end = shift @a_place;
			if( ($array2[1]>=$start && $array2[1]<=$end) || ($array2[2]>=$start && $array2[2]<=$end)){
				print OUT $array2[0]."\t".$array2[1]."\t".$array2[2]."\t".$start."\t".$end."\n";
			}
		}
	}
}
